<?php

use yii\db\Migration;

/**
 * Class m240928_112135_user_log
 */
class m240928_112135_user_log extends Migration
{

    const TB_USER_lOG = '{{%user_log}}'; // 用户日志表

    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $options = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $options = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB AUTO_INCREMENT=1';
        }

        $this->createTable(self::TB_USER_lOG, [
            'id' => $this->primaryKey()->unsigned()->unique(),
            'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
            'user_nickname' => $this->string(20)->notNull()->defaultValue('')->comment('用户昵称'),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->notNull(),
        ], $options);

        // 创建手机号为索引
        $this->createIndex('idx-user_id', self::TB_USER_lOG, ['user_id']);
        // 创建昵称为索引
        $this->createIndex('idx-user_nickname', self::TB_USER_lOG, ['user_nickname']);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        echo "m240928_112135_user_log cannot be reverted.\n";

        return false;
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m240928_112135_user_log cannot be reverted.\n";

        return false;
    }
    */
}
